/*
 * SyncResource.java
 *
 * Created on April 12, 2007, 1:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.auth.service.app;

import java.sql.SQLException;
import java.util.UUID;
import java.util.logging.Level;
import org.atomojo.auth.service.db.AuthDB;
import org.atomojo.auth.service.db.Permission;
import org.atomojo.auth.service.db.Role;
import org.restlet.Request;
import org.restlet.data.CharacterSet;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;

/**
 *
 * @author alex
 */
public class RoleQueryResource extends ServerResource
{
   
   long expiration = 3600*1000;
   AuthDB db;
   String roleName;
   String roleId;
   String permissionName;
   String permissionId;
   /** Creates a new instance of SyncResource */
   public RoleQueryResource() {
      setNegotiated(false);
   }

   protected void doInit() {
      db = (AuthDB)getRequest().getAttributes().get(AuthApplication.DB_ATTR);
      roleName = AuthApplication.getStringAttribute(getRequest(),"name",null);
      roleId = AuthApplication.getStringAttribute(getRequest(),"uuid",null);
      permissionId = AuthApplication.getStringAttribute(getRequest(),"permission-id",null);
      permissionName = AuthApplication.getStringAttribute(getRequest(),"permission-name",null);
   }
   
   public Representation get()
   {
      try {
         Role role = fetchRole();
         if (role==null) {
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return new StringRepresentation("Role not found.");
         }
         if (permissionId==null && permissionName==null) {
            Representation entity = new DBObjectRepresentation(MediaType.APPLICATION_XML,role);
            entity.setCharacterSet(CharacterSet.UTF_8);
            getResponse().setStatus(Status.SUCCESS_OK);
            return entity;
         } else {
            Permission p = fetchPermission();
            if (p==null || !role.hasPermission(p)) {
               getResponse().setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
            } else {
               getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
            }
            return null;
         }
      } catch (IllegalArgumentException ex) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("Bad UUID value "+permissionId);
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Cannot get user data from database.",ex);
         getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
         return new StringRepresentation("Database error, see logs.");
      }
   }
   
   protected Role fetchRole()
      throws SQLException,IllegalArgumentException
   {
      Role role = null;
      if (roleName!=null) {
         role = db.getRole(roleName);
      }
      if (roleId!=null) {
         UUID id = UUID.fromString(roleId);
         role = db.getRole(id);
      }
      return role;
   }
  
   protected Permission fetchPermission()
      throws SQLException,IllegalArgumentException
   {
      Permission permission = null;
      if (permissionName!=null) {
         permission = db.getPermission(permissionName);
      }
      if (permissionId!=null) {
         UUID id = UUID.fromString(permissionId);
         permission = db.getPermission(id);
      }
      return permission;
   }
}
